package com.zbl.singleDemo;

import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author zbl1997
 * @date 2022/9/6
 * @desc
 */
public class 读取文件 {
    /**
     * 程序的入口点
     * 该方法首先调用javaLoad方法，然后输出分隔线，最后调用springLoad方法
     * @param args 命令行参数，本程序未使用
     * @throws Exception 如果javaLoad或springLoad方法抛出异常，则向上传递
     */
    public static void main(String[] args) throws Exception {
        javaLoad();
        // 输出分隔线，用于区分不同的执行阶段
        System.out.println("------");
        springLoad();
    }


    static void javaLoad() {
        try {
            String filePath = "src/main/resources/text/work.text";
            InputStreamReader inputStream = new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStream);
            StringBuffer sb = new StringBuffer();
            String line = null;
            while((line = bufferedReader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
            System.out.println(sb);
        } catch (IOException ignored) {
        }
    }

    static void springLoad() {
        try {
            String filePath = "src/main/resources/text/work.text";
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8);
            System.out.println(FileCopyUtils.copyToString(inputStreamReader));
        } catch (Exception ignored) {
        }
    }
}
